package com.nanhai.buddhist.college.module.study.controller.teacher;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.dto.ClassesInfoDTO;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.vo.ClassesInfoDTOPageVO;
import com.nanhai.buddhist.college.module.collegebase.api.student.StudentApi;
import com.nanhai.buddhist.college.module.collegebase.api.student.dto.StudentRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.vo.ClassesStudentListPageVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableRespVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementCheckReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.BatchAddClockRecordReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.ManagementCheckPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.StudentClockListReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.SubjectStudentClockResVO;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagement.AttendanceManagementConvert;
import com.nanhai.buddhist.college.module.study.convert.classclockrecord.ClockRecordConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.classclockrecord.ClockRecordDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.timetable.ClassTimetableDO;
import com.nanhai.buddhist.college.module.study.enums.ClockRoleTypeEnum;
import com.nanhai.buddhist.college.module.study.service.attendancemanagement.AttendanceManagementService;
import com.nanhai.buddhist.college.module.study.service.classclockrecord.ClockRecordService;
import com.nanhai.buddhist.college.module.study.service.timetable.ClassTimetableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

/**
 * @author AKai
 * @date 2022/8/24
 */
@Api(tags = "教师PC端 - 班级管理")
@RestController
@RequestMapping("/study/teacher")
@Validated
public class TeacherClassesManageController {

    @Resource
    private CollegeClassApi collegeClassApi;

    @Resource
    private StudentApi studentApi;

    @Resource
    private ClockRecordService clockRecordService;

    @Resource
    private AttendanceManagementService attendanceManagementService;

    @Resource
    private ClassTimetableService classTimetableService;


    @GetMapping("/classes/info")
    @ApiOperation("获取班级详情")
    public CommonResult<ClassesInfoDTO> getClassesInfoDTOById(@RequestParam("id") String id) {
        return collegeClassApi.getClassesInfoDTOById(id);
    }

    @PostMapping("/student/page")
    @ApiOperation("获取班级中学生列表")
    public CommonResult<PageResult<StudentRespDTO>> getClassesStudentList(@Valid ClassesStudentListPageVO req) {
        return studentApi.getStudentRespDTO(req);
    }


    @GetMapping("/student/clock/page")
    @ApiOperation("获取考勤列表--打卡记录")
    public CommonResult<PageResult<ClockRecordRespVO>> getClockRecordPage(@Valid ClockRecordPageReqVO pageVO) {
        pageVO.setRole(ClockRoleTypeEnum.STUDENT.getRole());
        PageResult<ClockRecordDO> pageResult = clockRecordService.getClockRecordPage(pageVO);
        return success(ClockRecordConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/byTeacher/List")
    @ApiOperation("根据教师id获取班级列表（分页）")
    public CommonResult<PageResult<ClassesInfoDTO>> getClassesInfoDTOListByTeacher(@Validated ClassesInfoDTOPageVO req) {
        return collegeClassApi.getClassesInfoDTOPageByTeacher(req);
    }


    @GetMapping("/leave/get/info")
    @ApiOperation("获取请假详情")
    public CommonResult<AttendanceManagementRespVO> getAttendanceManagement(@RequestParam("id") String id) {
        AttendanceManagementDO attendanceManagement = attendanceManagementService.getAttendanceManagement(id);
        return success(AttendanceManagementConvert.INSTANCE.convert(attendanceManagement));
    }

    @GetMapping("/leave/check/page")
    @ApiOperation("获取审批请假分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementCheckPage(@Valid ManagementCheckPageReqVO pageVO) {
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementCheckPage(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/leave/check/get")
    @ApiOperation("获得打卡记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<ClockRecordRespVO> getClockRecord(@RequestParam("id") String id) {
        ClockRecordDO clockRecord = clockRecordService.getClockRecord(id);
        return success(ClockRecordConvert.INSTANCE.convert(clockRecord));
    }

    @PutMapping("/leave/check")
    @ApiOperation("请假审批")
    public CommonResult<Boolean> checkAttendanceLeave(@Valid @RequestBody AttendanceManagementCheckReqVO pageVO) {
        attendanceManagementService.checkAttendanceLeave(pageVO);
        return CommonResult.success(true);
    }

    @GetMapping("/getClassTimetableListByTeacherId")
    @ApiOperation("根据教师ID获得课程列表")
    public CommonResult<List<ClassTimetableRespVO>> getClassTimetableListByTeacherId(@RequestParam("teacherId") String teacherId) {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableListByTeacherId(teacherId);
        return success(classTimetableService.coverNames(list));
    }

    @PostMapping("/batchAddClockRecord")
    @ApiOperation("批量新增打卡记录")
    public CommonResult<Boolean> batchAddClockRecord(@Validated @RequestBody BatchAddClockRecordReqVO batchAddClockRecordReqVO) {
        clockRecordService.batchAddClockRecord(batchAddClockRecordReqVO);
        return success(true);
    }


    @GetMapping("/getQrCodeStr")
    @ApiOperation("获取生成二维码字符串")
    public CommonResult<String> getQrCodeStr(Long id) {
        return success(classTimetableService.getQrCodeStr(id));
    }


    @GetMapping("/getStudentClockList")
    @ApiOperation("获取课程中学生的打卡详情 ")
    public CommonResult<List<SubjectStudentClockResVO>> getStudentClockList(@Valid StudentClockListReqVO reqVO) {
        return success(clockRecordService.getStudentClockList(reqVO));
    }


}
